package io.renren.common.utils;

import java.util.Random;
import java.util.UUID;

/**
 * 生成随机码
 */
public class MyRandomCode {

    /**
     * 随机生成字母
     *
     * @return
     */
    public static String genRandomNum() {
        int maxNum = 36;
        int i;
        int count = 0;
        char[] str = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
        StringBuffer pwd = new StringBuffer("");
        Random r = new Random();
        while (count < 3) {
            i = Math.abs(r.nextInt(maxNum));
            if (i >= 0 && i < str.length) {
                pwd.append(str[i]);
                count++;
            }
        }
        return pwd.toString();
    }

    public static String[] chars = new String[]{"a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};


    public static String generateShortUuid(int num) {
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < num; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString();
    }

  /*  public static void main(String[] args) {
        String Uuid = RandomlyGeneratedCode.generateShortUuid(6);
        System.out.println(Uuid);
    }*/

    /**
     * 随机生成 用户昵称
     *
     * @return 字符串
     */
    public static String getNickname() {
        String s = genRandomNum();
        String uuid = MyRandomCode.generateShortUuid(6);
        return s + "_" + uuid;
    }

    /**
     * 随机码英文加数字
     *
     * @return 随机码
     */
    public static String generateCode() {
        String charList = "ABCDEFGHIJKLMNPQRSTUVWXY";
        String numList = "0123456789";
        String rev = "";
        int maxNumCount = 4;
        int length = 6;
        Random f = new Random();
        for (int i = 0; i < length; i++) {
            if (f.nextBoolean() && maxNumCount > 0) {
                maxNumCount--;
                rev += numList.charAt(f.nextInt(numList.length()));
            } else {
                rev += charList.charAt(f.nextInt(charList.length()));
            }
        }
        return rev;
    }

    /**
     * 随机生成8位数字
     *
     * @return 随机码
     */
    public static String generateNumbers() {
        int number = (int) ((Math.random() * 9 + 1) * 10000000);
        return String.valueOf(number);
    }

    public static String generateRandomCode() {
        //产生一个能存到五个元素的数组
        int[] arr = new int[5];
        for (int i = 0; i < arr.length; i++) {
            //为了防止默认0被当做检查重复的对象所以全部设为-1
            arr[i] = -1;
        }
        //index用来做索引值用于数组的迭代
        int index = 0;
        while (index < arr.length) {
            Random rd = new Random();
            //生成0~10之间的随机数  11
            int num = rd.nextInt(9);
            //judge自定义方法用来判断生成的值是否为数组内重复的元素 如果是 则重新生成
            if (judge(arr, num)) {
                //如果数组中没有重复的元素就放入数组内 并且索引值++
                arr[index++] = num;
            }
        }
        StringBuffer pwd = new StringBuffer("");
        for (int i = 0; i < arr.length; i++) {
            //System.out.println(arr[i]);
            pwd.append(arr[i]);

        }
        return pwd.toString();
    }

    /**
     * 判断生成的值是否为数组内重复的元素 如果是 则重新生成
     *
     * @param arr
     * @param num
     * @return
     */
    public static boolean judge(int[] arr, int num) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == num) {
                return false;
            }
        }
        return true;
    }


}
